<?php
class Common_Service_Country
{

    public static function add($row){
        $obj = new Common_Model_Country();
        return $obj->add($row);
	}

	public static function update($row,$value,$field='country_id'){
        $obj = new Common_Model_Country();
        return $obj->update($row,$value,$field);
	}


	public static function delete($value,$field='country_id'){
        $obj = new Common_Model_Country();
        return $obj->delete($value,$field);

	}


	public static function getByValue($value,$field='country_id'){
        $obj = new Common_Model_Country();
        return $obj->getByValue($value,$field);
	}

    public static function getByCondition($condition,$type='*',$orderby="",$page=0,$pageSize=0){
        $obj = new Common_Model_Country();
        return $obj->getByCondition($condition,$type,$orderby,$page,$pageSize);
	}


    public static function validator($val) {
        $error = array();
        $validateArr = array();
        $validateArr[] = array('name' => '国家中文名', 'value' => $val['cn_name'], 'regex' => array('require', 'length[0,255]', ));
        $validateArr[] = array('name' => '国家英文名', 'value' => $val['en_name'], 'regex' => array('require','length[0,255]','noCharacter'));
        $validateArr[] = array('name' => '排序序号', 'value' => $val['sort'], 'regex' => array('positive'));
        $error = Common_Service_Validator::formValidator($validateArr);

        if($val['country_id']>0 && !empty($val['cn_name'])){
            $condition = array(
                'cn_name' =>$val['cn_name'],
                'country_id' =>$val['country_id'],
            );
            $shipTypeList = self::getByCondition($condition);
            if(!empty($shipTypeList)){
                $error[] ="国家{$val['cn_name']}已经存在";
            }
        }
        if($val['country_id']>0 && !empty($val['en_name'])){
            $condition = array(
                'en_name' =>$val['en_name'],
                'country_id' =>$val['country_id'],
            );
            $shipTypeList = self::getByCondition($condition);
            if(!empty($shipTypeList)){
                $error[] ="国家{$val['en_name']}已经存在";
            }
        }
        return $error;

    }
}